package com.elektronikforumet.camalert;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;

public class GoogleMapActivity extends MapActivity 
{	
	private static MapView mapView;
	private static MapController mc;
	private static GeoPoint geoPoint;
	private static Drawable marker; 
	private static boolean pause = true;
	
	@Override
	public void onPause() {
		super.onPause();
		pause = true;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		pause = false;
		SetLocation(GpsListener.getLocation(), GpsListener.getCameras());
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.googlemap); 
				
		mapView = (MapView) findViewById(R.id.mapview1);
		mc = mapView.getController();
		mc.setZoom(16);
		
		mapView.setBuiltInZoomControls(true);

		//Set camera icon
		marker = getResources().getDrawable(R.drawable.icon); 
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}		
	
	public static void SetLocation(Location loc, ArrayList<SpeedCamera> cl) {
		if (mc != null && mapView != null && !pause) {
			geoPoint = new GeoPoint(
					(int) (loc.getLatitude() * 1E6), 
					(int) (loc.getLongitude() * 1E6));

			// Move mapView to this location 
			//
			mc.animateTo(geoPoint);

			// Clear and add new overlays
			//
			mapView.getOverlays().clear();

			if (cl != null) {
				for(SpeedCamera cam : cl) {
					MapCamOverlay markrs = new MapCamOverlay(
							marker, 
							new GeoPoint(
									(int)(cam.getLocation().getLatitude() * 1E6), 
									(int)(cam.getLocation().getLongitude() * 1E6)),
							(cam.getDirection()+1));
				
					mapView.getOverlays().add(markrs);
				}
			}
			mapView.getOverlays().add(new MyPosOverlay(geoPoint, 0));
			mapView.invalidate();
		}
	}
}